﻿using AssemblyVerifier;
using EmitDebugging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Spackle.Testing;
using System;
using System.Diagnostics;
using System.Diagnostics.SymbolStore;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

namespace EmitDebugging.Tests
{
	[TestClass]
	public sealed class TypeDescriptorTypeBuilderTests : CoreTests
	{
		[ClassInitialize]
		public static void ClassInitialize(TestContext context)
		{
			var assemblyName = new AssemblyName();
			assemblyName.Name = "TypeDescriptorTypeBuilderTests";
			var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
				assemblyName, AssemblyBuilderAccess.Save);

			var module = assembly.DefineDynamicModule(assemblyName.Name,
				assemblyName.Name + ".dll", true);

			var genericType = module.DefineType(
				assembly.GetName().Name + ".GenericType",
				TypeAttributes.Class | TypeAttributes.Sealed |
				TypeAttributes.Public, typeof(object));
			genericType.DefineGenericParameters("T", "U", "V");
			genericType.CreateType();

			var standardType = module.DefineType(
				assembly.GetName().Name + ".StandardType",
				TypeAttributes.Class | TypeAttributes.Sealed |
				TypeAttributes.Public, typeof(object));
			standardType.CreateType();

			TypeDescriptorTypeBuilderTests.GenericType = genericType;
			TypeDescriptorTypeBuilderTests.StandardType = standardType;
		}
		
		[TestMethod]
		public void GetGenericTypeDescription()
		{
			Assert.AreEqual(
				"class TypeDescriptorTypeBuilderTests.GenericType`3<T, U, V>", 
				new TypeDescriptor(TypeDescriptorTypeBuilderTests.GenericType).Value);
		}

		[TestMethod]
		public void GetStandardTypeDescription()
		{
			Assert.AreEqual("class TypeDescriptorTypeBuilderTests.StandardType",
				new TypeDescriptor(TypeDescriptorTypeBuilderTests.StandardType).Value);
		}
		
		private static TypeBuilder GenericType
		{
			get;
			set;
		}

		private static TypeBuilder StandardType
		{
			get;
			set;
		}
	}
}
